package com.narbeshuber.tablet.lasalara.fragment;

import java.util.LinkedList;

import android.app.ActionBar;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.narbeshuber.tablet.lasalara.LasaLaraActivity;
import com.narbeshuber.tablet.lasalara.R;
import com.narbeshuber.tablet.lasalara.Utils;
import com.narbeshuber.tablet.lasalara.fragment.InfoFragment.LayoutType;
import com.narbeshuber.tablet.lasalara.http.GetQuestionsTask;
import com.narbeshuber.tablet.lasalara.listadapters.ChapterAdapter;
import com.narbeshuber.tablet.lasalara.model.Chapter;
import com.narbeshuber.tablet.lasalara.model.Question;

public class ChapterFragment extends ListFragment {

	private static ChapterFragment chapterFragment;
	
	public static boolean resetMode = false; 
	public static Chapter chapter;
	
	
	private ChapterFragment(){
	}
	
	public static ChapterFragment getInstance(){
		if(chapterFragment==null){
			chapterFragment = new ChapterFragment();
		}
		return chapterFragment;
	}
	
	public ChapterAdapter getListAdapter(){
		return (ChapterAdapter)super.getListAdapter();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.chapterlayout, container, false);
		setHasOptionsMenu(true);
		return v;
	}
	
	@Override
	public void onStart() {
		getActivity().invalidateOptionsMenu();
		getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
		getActivity().getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME, ActionBar.DISPLAY_SHOW_HOME);
		getListView().setSelection(0);
		super.onStart();
	};
	
	/*@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		inflater.inflate(R.menu.lasamenu, menu);
		super.onCreateOptionsMenu(menu, inflater);
	}*/

	
	@Override
	public void onPrepareOptionsMenu(final Menu menu) {
		MenuItem item = menu.findItem(R.id.clear_progress);
		item.setEnabled(true);
		item.setVisible(true);
		item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
					
					public boolean onMenuItemClick(MenuItem item) {
						MenuItem infoitem = menu.findItem(R.id.info);
						infoitem.setIcon(R.drawable.ic_menu_info);
						if (getActivity().findViewById(R.id.right_top).getVisibility() != View.GONE){
							((LasaLaraActivity)getActivity()).hideInfo();
						}
						
						resetMode = !resetMode;
						if (resetMode) {
							item.setIcon(R.drawable.ic_menu_revert_active);
						} else {
							item.setIcon(R.drawable.ic_menu_revert);
						}
						getListAdapter().notifyDataSetChanged();
						return true;
					}
				});
		item = menu.findItem(R.id.info);
		item.setEnabled(true);
		item.setVisible(true);
		item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
		
			public boolean onMenuItemClick(MenuItem item) {
				FragmentManager fragmentManager = getFragmentManager();
		        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
				if (getActivity().findViewById(R.id.right_top).getVisibility() == View.GONE){
			        fragmentTransaction.replace(R.id.right_top, new InfoFragment(LayoutType.CHAPTER));
		        	fragmentTransaction.commit();
		        	getActivity().findViewById(R.id.right).setVisibility(View.GONE);
		        	getActivity().findViewById(R.id.right_top).setVisibility(View.VISIBLE);
		        	item.setIcon(R.drawable.ic_menu_info_active);
		        	return true;
				} else {
					fragmentTransaction.remove(fragmentManager.findFragmentById(R.id.right_top));
		        	fragmentTransaction.commit();
		        	getActivity().findViewById(R.id.right).setVisibility(View.VISIBLE);
		        	getActivity().findViewById(R.id.right_top).setVisibility(View.GONE);
		        	item.setIcon(R.drawable.ic_menu_info);
					return true;
				}
			}
		
		});
		item = menu.findItem(R.id.night_mode);
		item.setEnabled(true);
		item.setVisible(true);
		item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			
			public boolean onMenuItemClick(MenuItem item) {
				MenuItem infoitem = menu.findItem(R.id.info);
				infoitem.setIcon(R.drawable.ic_menu_info);
				if (getActivity().findViewById(R.id.right_top).getVisibility() != View.GONE){
					((LasaLaraActivity)getActivity()).hideInfo();
				}
				LasaLaraActivity.ninjaMode = !LasaLaraActivity.ninjaMode;
				if(LasaLaraActivity.ninjaMode){
					((LasaLaraActivity)getActivity()).setNightMode();
					item.setIcon(R.drawable.ic_menu_nightmode_active);
				} else {
					((LasaLaraActivity)getActivity()).setDayMode();
					item.setIcon(R.drawable.ic_menu_nightmode);
				}
				getListAdapter().notifyDataSetChanged();
				return true;
			}
		});
		
		item = menu.findItem(R.id.sponsor);
		item.setEnabled(true);
		item.setVisible(true);
		//super.onPrepareOptionsMenu(menu);
	}
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		if (getActivity().findViewById(R.id.right_top).getVisibility() != View.GONE){
			((LasaLaraActivity)getActivity()).hideInfo();
		}
		chapter = (Chapter) getListAdapter().getItem(position);

		LinkedList<Question> questions = ((LasaLaraActivity) getActivity())
				.getDbHelper().getAllQuestionsFromChapter(
						chapter.getChapterKey());
		((LasaLaraActivity) getActivity()).getPrefsHelper().saveChapterKey((chapter.getChapterKey()));

		if (questions.isEmpty()) {
			if(Utils.isOnline((LasaLaraActivity)getActivity())){
				new GetQuestionsTask((LasaLaraActivity) getActivity(), chapter)
						.execute();
			}
		} else {

			FragmentManager fragmentManager = (FragmentManager) getActivity()
					.getFragmentManager();
			FragmentTransaction fragmentTransaction = fragmentManager
					.beginTransaction();
			QuestionFragment questionFragment = new QuestionFragment((LasaLaraActivity)getActivity(), questions);
			fragmentTransaction
					.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
			fragmentTransaction.replace(R.id.right, questionFragment);
			fragmentTransaction.commit();

		}
		getListAdapter().notifyDataSetChanged();

	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.clear_progress:
			// TODO! kui ei ole selles vaates siis:
			item.setIcon(R.drawable.ic_menu_revert_active);
			// MUidu:
			// item.setIcon(R.drawable.ic_menu_revert);
			// Muu loogika ka muidugi
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

}
